<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');

/*
* Time-saving functions (:
*/
function textSetting($constant_name,&$settings,&$settingstatus) {
	$settingstatus[$constant_name]=NULL;
	eval('$settings[$constant_name]='.$constant_name.';
	$notSame=$_REQUEST[$constant_name]!='.$constant_name.'?true:false;');
	if(isset($_REQUEST[$constant_name])&&$notSame) {
		$settingstatus[$constant_name]=true;
		if(strlen($_REQUEST[$constant_name])!=0)
			$settings[$constant_name]=$_REQUEST[$constant_name];
		else
			$settingstatus[$constant_name]=false;
	}
}

function intSetting($constant_name,&$settings,&$settingstatus) {
	$settingstatus[$constant_name]=NULL;
	eval('$settings[$constant_name]='.$constant_name.';
	$notSame=$_REQUEST[$constant_name]!='.$constant_name.'?true:false;');
	if(is_numeric($_REQUEST[$constant_name])&&$notSame) {
		$settingstatus[$constant_name]=true;
		if($_REQUEST[$constant_name]>0)
			$settings[$constant_name]=$_REQUEST[$constant_name];
		else
			$settingstatus[$constant_name]=false;
	}
}

function anyintSetting($constant_name,&$settings,&$settingstatus) {
	$settingstatus[$constant_name]=NULL;
	eval('$settings[$constant_name]='.$constant_name.';
	$notSame=$_REQUEST[$constant_name]!='.$constant_name.'?true:false;');
	if(is_numeric($_REQUEST[$constant_name])&&$notSame) {
		$settingstatus[$constant_name]=true;
		$settings[$constant_name]=$_REQUEST[$constant_name];
	}
}

function boolSetting($constant_name,&$settings,&$settingstatus) {
	eval('$settings[$constant_name]='.$constant_name.';');
	$settingstatus[$constant_name]=NULL;
	if(isset($_REQUEST[$constant_name])) {
		$settings[$constant_name]=$_REQUEST[$constant_name]=='1';
		$settingstatus[$constant_name]=true;
	}
}

function textInput($constant_name,&$settings,&$settingstatus) {
	echo('
		<input type="text" name="'.$constant_name.'" class="'.(
		$settingstatus[$constant_name]===true?'verified':
		($settingstatus[$constant_name]===false?'failed':'noinput')
		).
		'" value="'.htmlentities($settings[$constant_name],ENT_QUOTES,'UTF-8').'" />
	');
}

function textareaInput($constant_name,&$settings,&$settingstatus) {
	echo('
		<textarea name="'.$constant_name.'" rows="5" cols="60" class="'.(
		$settingstatus[$constant_name]===true?'verified':
		($settingstatus[$constant_name]===false?'failed':'noinput')
		).
		'">'.htmlentities($settings[$constant_name],ENT_QUOTES,'UTF-8').'</textarea>
	');
}

function intInput($constant_name,&$settings,&$settingstatus) {
	echo('
		<input type="text" name="'.$constant_name.'" class="'.(
		$settingstatus[$constant_name]===true?'verified':
		($settingstatus[$constant_name]===false?'failed':'noinput')
		).
		'" value="'.htmlentities($settings[$constant_name],ENT_QUOTES,'UTF-8').'" />
	');
}

function boolInput($constant_name,&$settings,&$settingstatus) {
	echo('
		<input type="radio" name="'.$constant_name.'" '.
		($settings[$constant_name]?'checked="checked" ':'').
		' value="1" />On<br />
		
		<input type="radio" name="'.$constant_name.'" '.
		(!$settings[$constant_name]?'checked="checked" ':'').
		'value="0" />Off<br />
	');
}

/*
* Obtain text field settings
*/
$settings=array();
$settingstatus=array(); //Fail/pass settings

//Add and verify settings
textSetting('SITE_TITLE',$settings,$settingstatus);
textSetting('SITE_URL',$settings,$settingstatus);
textSetting('SITE_EMAIL',$settings,$settingstatus);
textSetting('DIE_MSG',$settings,$settingstatus);
textSetting('SITE_TITLE',$settings,$settingstatus);
textSetting('THEME',$settings,$settingstatus);
textSetting('THEMEFOLDER',$settings,$settingstatus);
textSetting('TITLE_DESC',$settings,$settingstatus);
textSetting('META_DESC',$settings,$settingstatus);
textSetting('RSS_PATH',$settings,$settingstatus);
textSetting('BLOGROLL_PATH',$settings,$settingstatus);
textSetting('PURE_VERSION',$settings,$settingstatus);
textSetting('UPLOAD_PATH',$settings,$settingstatus);
anyintSetting('DEFAULT_PAGE',$settings,$settingstatus);
intSetting('POSTS_PER_PAGE',$settings,$settingstatus);
intSetting('POSTS_PER_LIST',$settings,$settingstatus);
intSetting('COMMENTS_PER_LIST',$settings,$settingstatus);
intSetting('PREVIOUS_PAGES',$settings,$settingstatus);
intSetting('NEXT_PAGES',$settings,$settingstatus);
intSetting('MINIMUM_COMMENT_INTERVAL',$settings,$settingstatus);
intSetting('MINIMUM_COMMENT_LENGTH',$settings,$settingstatus);
intSetting('MAXIMUM_COMMENT_LENGTH',$settings,$settingstatus);
intSetting('POSTS_PER_PAGE',$settings,$settingstatus);
intSetting('POSTS_PER_PAGE',$settings,$settingstatus);
intSetting('MAX_UPLOAD_BYTES',$settings,$settingstatus);
intSetting('RSS_MAX_ENTRIES',$settings,$settingstatus);
intSetting('RSS_TTL',$settings,$settingstatus);
intSetting('RSS_EXCERPT_LENGTH',$settings,$settingstatus);
boolSetting('HIDE_ADMIN',$settings,$settingstatus);
boolSetting('RSS_PARSE_BBCODE',$settings,$settingstatus);
boolSetting('COMMENTS_PARSE_BBCODE',$settings,$settingstatus);
boolSetting('USE_GESHI_POSTS',$settings,$settingstatus);
boolSetting('USE_GESHI_PAGES',$settings,$settingstatus);
boolSetting('USE_GESHI_COMMENTS',$settings,$settingstatus);
boolSetting('ALWAYS_AUTO_APPROVE_COMMENTS',$settings,$settingstatus);
boolSetting('IP_BLACKLIST_DENY',$settings,$settingstatus);
boolSetting('WORD_BLACKLIST_OVERRIDE',$settings,$settingstatus);
boolSetting('AUTO_APPROVE_COMMENTS',$settings,$settingstatus);
boolSetting('STRIP_EXTRA_COMMENT_WHITESPACE',$settings,$settingstatus);
boolSetting('PREVENT_ATTACHMENT_HOTLINKING',$settings,$settingstatus);

//Meta Keywords
$settings['META_KEYWORDS']=META_KEYWORDS;
$settingstatus['META_KEYWORDS']=NULL;
if(isset($_REQUEST['META_KEYWORDS'])) {
	$words=explode(',',$_REQUEST['META_KEYWORDS']);
	foreach($words as $index=>$word)
		$words[$index]=trim(str_replace(',','',$word));
	$words=array_unique($words);
	$blank=array_search('',$words);
	if($blank!==false)
		unset($words[$blank]);
	if(count($words)>0) {
		$wordstring=implode(',',$words);
		if($wordstring!=META_KEYWORDS) {
			$settings['META_KEYWORDS']=$wordstring;
			$settingstatus['META_KEYWORDS']=true;
		}
	}
	else {
		$settingstatus['META_KEYWORDS']=false;
	}
}

//Save data if given
$saved=false;

if(isset($_REQUEST['save'])) {
	$tmpFile=fopen('../options.php','w+');
	$settingsdata='<?php
/*File generated by Pure settings*/';
	foreach($settings as $name=>$value) {
		$settingsdata.="\n".'define(\''.$name.'\','.(is_bool($value)?($value?'true':'false'):'\''.str_replace('\'','\\\'',$value).'\'').');';
	}
	$settingsdata.='
?>';
	fwrite($tmpFile,$settingsdata);
	fclose($tmpFile);
	$saved=true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>All Settings</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="settings.php" class="breadcrumb">Edit Settings</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
		<h2>All Settings</h2>
		<?php
			$errors=false;
			$errorText='';
			if(count($settingstatus)>0) {
				foreach($settingstatus as $constant=>$value) {
					if($value===false) {
						$errorText.='Invalid value for '.$constant.'<br />';
						$errors=true;
					}
				}
			}
			if($errors)
				echo('<strong>One or more errors found in input:</strong><br />'.$errorText.'Changes for these values have not been applied.<br />');
		if($saved)
			echo('Settings saved.');
		?>
		<form action="settings.php" method="post">
		<h3>Site Description</h3>
			<h4>Site Title</h4>
			<span class="about">This is what appears in the header and title of your site</span><br />
			<?php textInput('SITE_TITLE',$settings,$settingstatus); ?>
			
			<h4>Site URL</h4>
			<span class="about">The address that your blog is located at</span><br />
			<?php textInput('SITE_URL',$settings,$settingstatus); ?>
			<br />
			
			<h4>Site Email</h4>
			<span class="about">The email address used in the RSS feed</span><br />
			<?php textInput('SITE_EMAIL',$settings,$settingstatus); ?>
			<br />
			
			<h4>Title Description</h4>
			<span class="about">A short description of your blog, inserted into the title of the main page</span><br />
			<?php textInput('TITLE_DESC',$settings,$settingstatus); ?>
			<br />
			
			<h4>Meta Description</h4>
			<span class="about">A longer description of your blog, inserted into the META tag</span><br />
			<?php textareaInput('META_DESC',$settings,$settingstatus); ?>
			<br />
			
			<h4>Keywords</h4>
			<span class="about">Words (separated by commas, duplicates removed) that your blog is related to, inserted into the META tag</span><br />
			<?php textInput('META_KEYWORDS',$settings,$settingstatus); ?>
			<br />
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>Display</h3>
			<h4>Default Page</h4>
			<span class="about">Enter the ID number of the page you want visitors to see when they first arrive at your site. Set a negative value to show the blog.</span><br />
			<?php intInput('DEFAULT_PAGE',$settings,$settingstatus); ?>
			
			<h4>Hide admin link</h4>
			<span class="about">Hide the link to the administration in the nav_link dynamic?</span><br />
			<?php boolInput('HIDE_ADMIN',$settings,$settingstatus); ?>
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>RSS Feed</h3>
			<h4>Max Entries</h4>
			<span class="about">Maximum number of previous posts to show</span><br />
			<?php intInput('RSS_MAX_ENTRIES',$settings,$settingstatus); ?>
			<br />
			
			<h4>Excerpt Length</h4>
			<span class="about">Maximum number of characters to show in the excerpt, 0 to show all</span><br />
			<?php intInput('RSS_EXCERPT_LENGTH',$settings,$settingstatus); ?>
			<br />
			
			<h4>Parse BBCode</h4>
			<span class="about">Parse BBCode in RSS?</span><br />
			<?php boolInput('RSS_PARSE_BBCODE',$settings,$settingstatus); ?>
			
			<h4>Rss Path</h4>
			<span class="about">File to save the rss feed in</span><br />
			<?php textInput('RSS_PATH',$settings,$settingstatus); ?>
			<br />
			
			<h4>TTL</h4>
			<span class="about">Time-To-Live in minutes</span><br />
			<?php intInput('RSS_TTL',$settings,$settingstatus); ?>
			<br />
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>Attachments</h3>
			<h4>Max Size</h4>
			<span class="about">Maximum size of uploaded attachments, in bytes</span><br />
			<?php intInput('MAX_UPLOAD_BYTES',$settings,$settingstatus); ?>
			<br />
			
			<h4>Prevent Hotlinking</h4>
			<span class="about">Do not allow direct links from other sites to access your files</span><br />
			<?php boolInput('PREVENT_ATTACHMENT_HOTLINKING',$settings,$settingstatus); ?>
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>Spam Protection</h3>
			<h4>Always Approve</h4>
			<span class="about">Approve all comments, regardless of the options below in this section</span><br />
			<?php boolInput('ALWAYS_AUTO_APPROVE_COMMENTS',$settings,$settingstatus); ?>
			
			<h4>Deny Blacklisted IPs</h4>
			<span class="about">When enabled, comments from blacklisted IPs will be denied instead of being entered into the approval queue</span><br />
			<?php boolInput('IP_BLACKLIST_DENY',$settings,$settingstatus); ?>
			
			<h4>Force Blacklisted Word Approval</h4>
			<span class="about">When enabled, comments with blacklisted words will always be entered into the approval queue</span><br />
			<?php boolInput('WORD_BLACKLIST_OVERRIDE',$settings,$settingstatus); ?>
			
			<h4>Auto Approve Trusted</h4>
			<span class="about">When enabled, comments from people with previously approved comments are automatically approved</span><br />
			<?php boolInput('AUTO_APPROVE_COMMENTS',$settings,$settingstatus); ?>
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>Comments</h3>
			<h4>Minimum Comment Interval</h4>
			<span class="about">The minimum time in seconds between comments from a single IP</span><br />
			<?php intInput('MINIMUM_COMMENT_INTERVAL',$settings,$settingstatus); ?>
			
			<h4>Minimum Comment Length</h4>
			<span class="about">The minimum number of alphanumeric characters in a comment</span><br />
			<?php intInput('MINIMUM_COMMENT_LENGTH',$settings,$settingstatus); ?>
			
			<h4>Maximum Comment Length</h4>
			<span class="about">The maximum number of any characters in a comment</span><br />
			<?php intInput('MAXIMUM_COMMENT_LENGTH',$settings,$settingstatus); ?>
			
			<h4>Parse BBCode</h4>
			<span class="about">Parse BBCode in comments?</span><br />
			<?php boolInput('COMMENTS_PARSE_BBCODE',$settings,$settingstatus); ?>
			
			<h4>Strip Excess Whitespace</h4>
			<span class="about">Removes excess whitespace like multiple consecutive new lines</span><br />
			<?php boolInput('STRIP_EXTRA_COMMENT_WHITESPACE',$settings,$settingstatus); ?>
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>Pagination</h3>
			<h4>Posts Per Page</h4>
			<span class="about">Posts to display per page on the main blog</span><br />
			<?php intInput('POSTS_PER_PAGE',$settings,$settingstatus); ?>
			
			<h4>Posts Per List</h4>
			<span class="about">Posts to display per page in the admin panel</span><br />
			<?php intInput('POSTS_PER_LIST',$settings,$settingstatus); ?>
			
			<h4>Comments Per List</h4>
			<span class="about">Comments to display per page in the admin panel</span><br />
			<?php intInput('COMMENTS_PER_LIST',$settings,$settingstatus); ?>
			
			<h4>Previous Pages</h4>
			<span class="about">Previous pages to display in the pagination before using a &quot;Last&quot; button</span><br />
			<?php intInput('PREVIOUS_PAGES',$settings,$settingstatus); ?>
			
			<h4>Next Pages</h4>
			<span class="about">Following pages to display in the pagination before using a &quot;First&quot; button</span><br />
			<?php intInput('NEXT_PAGES',$settings,$settingstatus); ?>
			<br />
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>GESHI</h3>
			<h4>Use in Posts</h4>
			<span class="about">If enabled, code tags in posts with syntax specified will be highlighted using GESHI.</span><br />
			<?php boolInput('USE_GESHI_POSTS',$settings,$settingstatus); ?>
			
			<h4>Use in Pages</h4>
			<span class="about">If enabled, code tags in pages with syntax specified will be highlighted using GESHI.</span><br />
			<?php boolInput('USE_GESHI_PAGES',$settings,$settingstatus); ?>
			
			<h4>Use in Comments</h4>
			<span class="about">If enabled, code tags in comments with syntax specified will be highlighted using GESHI.</span><br />
			<?php boolInput('USE_GESHI_COMMENTS',$settings,$settingstatus); ?>
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
			
		<h3>Template</h3>
			<h4>Theme Folder</h4>
			<span class="about">Where the theme folders are stored</span><br />
			<?php textInput('THEMEFOLDER',$settings,$settingstatus); ?>
			<br />
			
			<h4>Theme</h4>
			<span class="about">The name of the theme to use (<a href="template.php">use the theme viewer instead</a>)</span><br />
			<?php textInput('THEME',$settings,$settingstatus); ?>
			<br />
			<br />
			<input type="submit" name="save" value="Save"/>
			<br />
			<br />
		
		<h3>Should not be edited</h3>
			<h4>Upload Path</h4>
			<span class="about">Where files are stored</span><br />
			<?php textInput('UPLOAD_PATH',$settings,$settingstatus); ?>
			<br />
			
			<h4>Die Statement</h4>
			<span class="about">Used by some important files</span><br />
			<?php textInput('DIE_MSG',$settings,$settingstatus); ?>
			<br />
			
			<h4>Blogroll File</h4>
			<span class="about">File where blogroll is saved</span><br />
			<?php textInput('BLOGROLL_PATH',$settings,$settingstatus); ?>
			<br />
			
			<h4>Pure version</h4>
			<span class="about">Current Pure version</span><br />
			<?php textInput('PURE_VERSION',$settings,$settingstatus); ?>
			<br />
			<br />
			<input type="submit" name="save" value="Save"/>
		</form>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>